package com.dinglit.controller;

import com.dinglit.dao.UserMapper;
import com.dinglit.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by liuhuanchao on 2018/8/5.
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserMapper userMapper;

    /**
     * 查询详情
     * http://localhost:8080/user/selectUserById?id=1
     * @param id
     * @return
     */
    @RequestMapping(value={"/selectUserById"}, method= RequestMethod.GET)
    public User selectUserById(String id){
        User user = userMapper.selectUserById(Integer.parseInt(id));
        return user;
    }

    /**
     * 通过名称搜索
     * http://localhost:8080/user/selectUserByName?userName=123
     * @param userName
     * @return
     */
    @RequestMapping(value={"/selectUserByName"}, method=RequestMethod.GET)
    public List<User> selectUserByName(String userName){
        return userMapper.selectUserByName(userName);
    }

    /**
     * 添加
     * http://localhost:8080/user/addUser?userName=123&userAge=12&userAddress=456
     * @param user
     */
    @RequestMapping(value={"/addUser"}, method=RequestMethod.POST)
    public void addUser(User user){
        userMapper.addUser(user);
    }

    /**
     * 修改
     * http://localhost:8080/user/updateUser?userName=123&userAge=12&userAddress=456&id=1
     * @param user
     */
    @RequestMapping(value={"/updateUser"}, method=RequestMethod.POST)
    public void updateUser(User user){
        userMapper.updateUser(user);
    }

    /**
     * 删除
     * http://localhost:8080/user/deleteUser?id=1
     * @param id
     * @return
     */
    @RequestMapping(value={"/deleteUser"}, method=RequestMethod.POST)
    public void deleteUser(String id){
        userMapper.deleteUser(Integer.parseInt(id));
    }
}
